package com.smile.util;

import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.smile.auth.AuthUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Component
public class PasswordUtil {


    private static final String privateKey = "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEAkS6sOQLyapVTPq5q\n" +
            "ubuf2fX/VvmaVYlsQuyPSojjfJG9Jcpn4SYa96IffhKWvhbtzvezshcboYvqsfb5\n" +
            "2cp7CwIDAQABAkBPW5K6Kxk1F2CEX7Rox90tt+CVuyQYY2CxCDufA0pNuU1G7jcS\n" +
            "VAkGTR4dqUXV+BlWe6wQtwQtUuKjQqsyxkBpAiEAwVnbD2gQOoSBkY7AGJw8mfee\n" +
            "nUckXh1ZAHMNGFdgyPUCIQDAOU4DDM4EywoOGOOhIgDdcfbQy9f0+dqwD1pnBQOz\n" +
            "/wIgeqmv0OlbClTQAnEGq1ZLF/7A8NSWXOLPePqEf0HH1BUCIQCtTXzmBw9fBudj\n" +
            "2lCvn4aEIBFUIdPsC698d/sbUkwKZQIgC8mWOEyXGa6n8fND9+sjxG6cBS8mqgha\n" +
            "ZrJOURG94Zw=";
    @Autowired
    private PasswordEncoder passwordEncoder;

    private PasswordUtil() {
    }


    public String encode(String password) {
        return passwordEncoder.encode(password);
    }

    public String decodeRsaPassword(AuthUserEntity authUserEntity) {
        RSA rsa = new RSA(privateKey, null);
        return new String(rsa.decrypt(authUserEntity.getPassword(), KeyType.PrivateKey));
    }
}
